<?php
/**
 * 《职位信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class Position extends InfoUtil {

	public $tbl = Constant::tbl_position;

	public $orderby = array(
		'-' => '请选择',
		'code' => '编号',
		'name' => '名称',
		'orderby' => '排序',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}
	
	function layout($controller, $request) {
		$data = array(
			'职位信息' => array('cc'=>'info.php?do=Position.find', 'js'=>''),
			'员工职位' => array('cc'=>'emp.php?do=Position.find', 'js'=>''),
		);

		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/position/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$code_s = $request['code_s'];
		$name_s = $request['name_s'];

		$where = " WHERE 1=1";

		if (!empty($code_s)) {
			$where .= " AND code = '" . $code_s . "'";
		}

		if (!empty($name_s)) {
			$where .= " AND name = '" . $name_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/position/list');
		return true;
	}
	
	function xls($controller, $request) {
		$this->pretreat($controller);
	
		$this->findPara($controller, $request);
	
		$request['tpl'] = "xls";
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
	
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
	
			$row = $this->_orderby_x($controller, $row, $this->orderby_s);
	
			$data[] = $row;
		}
	
		$controller->assign_by_ref('result', $data);
	
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);
	
		$controller->display($request, 'info/position/list');
		
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/position/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$dept = $request['dept'];
		$name = $request['name'];

		$content = addslashes($request['content']);
		
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(dept, name, content, status, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $dept, $name, $content, $status, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
		
		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);
		
		$controller->assign_by_ref('sequence_option', range(0, 30, 1));

		$controller->display($request, 'info/position/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$dept = $request['dept'];
		$name = $request['name'];

		$content = addslashes($request['content']);

		$orderby = $request['orderby'];
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET dept = '%s', name = '%s', content = '%s', orderby = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $dept, $name, $content, $orderby, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/position/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row = $this->_view_t($row);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function importPost($controller, $request) {
		$this->pretreat($controller);

		$data = $request['data'];
		// echo $data;

		$aa = preg_split("/[\\n]+/i", $data);
		// print_r($a);

		$db = newdb($this->dsn);
		while (list($k, $v) = each($aa)) {
			$buf = trim($v);
			if (strlen($buf) < 5) {
				continue;
			}

			$a = preg_split("/[\t|\s]+/i", $buf);
			// print_r($a);

			$code = trim($a[0]);
			$name = trim($a[1]);


			$fmt = "INSERT INTO %s(`code`, name) VALUES('%s', '%s')";
			$sql = sprintf($fmt, $this->tbl, $code, $name);
			// debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
/**
	 * 权限分配
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function emp($controller, $request) {
		$this->pretreat($controller);
			
		$id = $request['id'];
		$position = $this->id($this->tbl, $id);

		$controller->assign_by_ref('position', $position);

		$db = newdb($this->dsn_r);
		$data = array();
		$fmt = "SELECT * FROM %s WHERE status = '1' ORDER BY id DESC";
		$sql = sprintf($fmt, Constant::tbl_emp);
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if ($this->emp_position($row['id'], $position['id'])) {
				$row['class'] = "class='added'";
				$data[] = $row;
			}

			$data_all[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('result_all', $data_all);

		$controller->display($request, 'info/position/emp');
		return true;
	}

	function emp_position($emp, $position) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '1' AND emp = '%s' AND position = '%s'";
		$sql = sprintf($fmt, Constant::tbl_emp_position, $emp, $position);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($row['status'] == '1') {
				return true;
			}
		}

		return false;
	}

	/**
	 * 权限分配
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function empPost($controller, $request) {
		$id = $request['id'];
		
		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '2' WHERE position = '%s'";
		$sql = sprintf($fmt, Constant::tbl_emp_position, $id);
		debug($sql);
		$db->query($sql);

		$list = $request['list'];
		if (empty($list)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));
			$this->findPost($controller, $request);
			return true;
		}

		$bb = explode(',', $list);
		while (list($k, $emp) = each($bb)) {
			$fmt = "SELECT * FROM %s WHERE emp = '%s' AND position = '%s'";
			$sql = sprintf($fmt, Constant::tbl_emp_position, $emp, $id);
			$result = $db->query($sql);
			if ($db->num_rows($result) > 0) {
				$fmt = "UPDATE %s SET status = '1', modifytime = now() WHERE emp = '%s' AND position = '%s'";
				$sql = sprintf($fmt, Constant::tbl_emp_position, $emp, $id);
			} else {
				$fmt = "INSERT INTO %s(emp, position, createtime, modifytime) VALUES('%s', '%s', now(), now())";
				$sql = sprintf($fmt, Constant::tbl_emp_position, $emp, $id);
			}
			debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
}
?>